package com.stylefeng.guns.rest.modular.user;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.api.user.UserApi;
import com.stylefeng.guns.api.user.UserInfoModer;
import com.stylefeng.guns.api.user.UserModel;
import com.stylefeng.guns.rest.common.CurrentUser;
import com.stylefeng.guns.rest.modular.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author zhaoluowei
 * @Date 2019/9/11  0:02
 * @Version 1.0
 *
 * 映射注解 不要用name 要用value
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Reference(interfaceClass = UserApi.class,check = false)
    private UserApi userApi;

    /**
     * @param userModel 用户名、密码是关键的
     * @return
     */
    @PostMapping(value = "/register")
    public ResponseVo register(UserModel userModel) {
        if (userModel.getUsername() == null || userModel.getUsername().trim().length() == 0) {
            return ResponseVo.serviceFail("用户名不能为空");
        }
        if (userModel.getPassword() == null || userModel.getPassword().trim().length() == 0) {
            return ResponseVo.serviceFail("密码不能为空");
        }

        boolean isSuccess = userApi.register(userModel);
        if (isSuccess) {
            return ResponseVo.success("注册成功");
        }
        return ResponseVo.success("注册失败");
    }

    /**
     * 只能发 post 表单请求
     * @param username
     * @return
     */
    @PostMapping(value = "/check")
    public ResponseVo check(String username) {
        //String username = jsonObject.getString("username");

        if (username != null && username.trim().length() > 0) {
            //当返回 true 的时候，表示用户名可用
            boolean notExists = userApi.checkUsername(username);
            if (notExists) {
                return ResponseVo.success("用户名不存在");
            } else {
                return ResponseVo.serviceFail("用户名已存在");
            }
        }
        return ResponseVo.success("用户名不能为空");
    }


    @PostMapping(value = "/logout", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVo logout(String username) {
        /**
         * 应用：
         *  1.前端存储jwt 7 天  jwt的刷新
         *  2.服务器存储活跃用户信息
         *  3.jwt 里的userId为key 查找活跃用户
         *
         * 退出：
         *  1.前端删除jwt
         *  2.后端服务器删除活跃用户缓存
         *
         * 解决方案：
         *  前端删除
         *
         */

        return ResponseVo.success("用户退出成功");
    }


    @PostMapping(value = "/getUserInfo")
    public ResponseVo getUserInfo() {
        System.out.println("获取用户信息----> 我进来了");

        String currentUser = CurrentUser.getCurrentUserInfoModer();
        if (currentUser != null || currentUser.trim().length() > 0) {
            //查询当前的用户信息
            int userId = Integer.parseInt(currentUser);
            UserInfoModer userInfo = userApi.getUserInfo(userId);
            if (userInfo != null) {
                return ResponseVo.success(userInfo);
            }
            return ResponseVo.serviceFail("用户信息查询失败");
        }
        return ResponseVo.serviceFail("当前用户尚未登录");
    }


    @PostMapping(value = "/updateUserInfo")
    public ResponseVo updateUserInfo(UserInfoModer userInfoModer) {
        String currentUser = CurrentUser.getCurrentUserInfoModer();
        if (currentUser != null || currentUser.trim().length() > 0) {
            //查询当前的用户信息
            int userId = Integer.parseInt(currentUser);
            //判断当前登录人的id  和传入的id是否一致
            if (userId == userInfoModer.getUuid()) {
                UserInfoModer userInfo = userApi.updateUserInfo(userInfoModer);
                if (userInfo != null) {
                    return ResponseVo.success(userInfo);
                }
                return ResponseVo.serviceFail("用户信息修改失败");

            }
            return ResponseVo.serviceFail("用户信息数据被篡改");


        }
        return ResponseVo.serviceFail("当前用户尚未登录");
    }


}
